<template>
  <a-drawer width="600" :title="title" :visible="open" @close="handleClose">
    <a-form-model ref="formRef" :model="form" :rules="rules" :labelCol="{span: 4}" :wrapperCol="{span: 20}">
      <a-form-model-item label="定时任务ID" prop="jobId">
        <a-input v-model="form.jobId" placeholder="请输入定时任务ID" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="任务标题" prop="title">
        <a-input v-model="form.title" placeholder="请输入任务标题" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="任务名称" prop="jobName">
        <a-input v-model="form.jobName" placeholder="请输入任务名称" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="任务组名" prop="jobGroup">
        <a-input v-model="form.jobGroup" placeholder="请输入任务组名" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="参数" prop="params">
        <a-input v-model="form.params" placeholder="请输入参数" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="cron表达式" prop="cron">
        <a-input v-model="form.cron" placeholder="请输入cron表达式" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="状态码" prop="code">
        <a-input v-model="form.code" placeholder="请输入状态码" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="耗时(ms)" prop="time">
        <a-input v-model="form.time" placeholder="请输入耗时(ms)" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="执行结果" prop="result">
        <a-input v-model="form.result" placeholder="请输入执行结果" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark">
        <a-input v-model="form.remark" placeholder="请输入备注" type="textarea" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="创建时间" prop="createTime">
        <a-input v-model="form.createTime" placeholder="请输入创建时间" allow-clear/>
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="handleSubmit">保存</a-button>
          <a-button type="dashed" @click="handleClose">取消</a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>
<script>
import {getJobLog, saveJobLog, updateJobLog} from '@/api/quartz/jobLog'

export default {
  data() {
    return {
      submitLoading: false,
      title: '',
      open: false,
      // 表单
      form: {},
      rules: {
        jobId: [{required: false, trigger: ['blur', 'change'], message: '请输入定时任务ID'}],
        title: [{required: false, trigger: ['blur', 'change'], message: '请输入任务标题'}],
        jobName: [{required: false, trigger: ['blur', 'change'], message: '请输入任务名称'}],
        jobGroup: [{required: false, trigger: ['blur', 'change'], message: '请输入任务组名'}],
        params: [{required: false, trigger: ['blur', 'change'], message: '请输入参数'}],
        cron: [{required: false, trigger: ['blur', 'change'], message: '请输入cron表达式'}],
        code: [{required: false, trigger: ['blur', 'change'], message: '请输入状态码'}],
        time: [{required: false, trigger: ['blur', 'change'], message: '请输入耗时(ms)'}],
        result: [{required: false, trigger: ['blur', 'change'], message: '请输入执行结果'}],
        remark: [{required: false, trigger: ['blur', 'change'], message: '请输入备注'}],
        createTime: [{required: false, trigger: ['blur', 'change'], message: '请输入创建时间'}]
      }
    }
  },
  methods: {
    /** 点击关闭 */
    handleClose() {
      this.reset()
      this.open = false
    },
    /** 表单重置 */
    reset() {
      this.$refs.formRef && this.$refs.formRef.resetFields()
      this.form = {
        id: undefined,
        jobId: undefined,
        title: undefined,
        jobName: undefined,
        jobGroup: undefined,
        params: undefined,
        cron: undefined,
        code: undefined,
        time: undefined,
        result: undefined,
        remark: undefined,
        createTime: undefined
      }
    },
    /** 点击新增 */
    handleAdd() {
      this.reset()
      this.title = '添加定时任务日志'
      this.open = true
    },
    /** 点击修改 */
    handleUpdate(row) {
      this.reset()
      getJobLog(row.id).then(res => {
        this.title = '修改定时任务日志'
        this.open = true
        this.$nextTick(() => {
          this.form = res.data
        })
      })
    },
    /** 点击保存 */
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id && this.form.id > 0) {
            updateJobLog(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            saveJobLog(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        }
      })
    }
  }
}
</script>
